/**
 * 产品
 * 控制器
 */
const Product_sql = require("../model/product");
const Category_sql = require("../model/category");
const product = {
  // 获取产品列表
  async product_list(req, res) {
    const { page, limit } = req.query;
    const offset = (page - 1) * limit;
    try {
      const result = await Product_sql.findAndCountAll({
        offset: offset,
        limit: limit,
        order: [["create_time", "DESC"]],
      });
      res.json({ code: 1, message: "获取产品列表成功", data: result });
    } catch (error) {
      console.error(error);
      res.json({ code: 500, message: "服务器错误" });
    }
  },
  // 获取产品详情
  async product_detail(req, res) {
    const { product_id } = req.query;
    try {
      const result = await Product_sql.findByPk(product_id);
      if (!result) {
        return res.json({ code: -1, message: "产品不存在" });
      }
      res.json({ code: 1, message: "获取产品详情成功", data: result });
    } catch (error) {
      console.error(error);
      res.json({ code: 500, message: "服务器错误" });
    }
  },
  // 新增产品
  async product_add(req, res) {
    const { category_id, title, content, params, video_url, spece_book_url, product_imgs } = req.body;
    try {
      const isCategory = await Category_sql.findByPk(category_id);
      if (!isCategory) {
        return res.json({ code: -1, message: "分类不存在" });
      }
      // 判断产品是否已存在
      const isProduct = await Product_sql.findOne({ where: { title: title } });
      if (isProduct) {
        return res.json({ code: -1, message: "产品已存在" });
      }
      const result = await Product_sql.create({
        category_id: category_id,
        title: title,
        content: content,
        params: params,
        video_url: video_url,
        spece_book_url: spece_book_url,
        product_imgs: product_imgs,
      });
      if (!result) {
        return res.json({ code: -1, message: "新增产品失败" });
      }
      res.json({ code: 1, message: "新增产品成功" });
    } catch (error) {
      console.error(error);
      res.json({ code: 500, message: "服务器错误" });
    }
  },
  // 修改产品
  async product_update(req, res) {
    console.log(req.body);
    
    const { product_id, category_id, title, content, params, video_url, specs_book_url, product_imgs } = req.body;
    try {
      // 判断产品是否存在
      const record = await Product_sql.findByPk(product_id);
      if (!record) {
        return res.json({ code: -1, message: "产品不存在" });
      }
      // 判断分类是否存在
      const isCategory = await Category_sql.findByPk(category_id);
      if (!isCategory) {
        return res.json({ code: -1, message: "分类不存在" });
      }
      // 判断哪些字段需要更新 使用循环
      const bodyParams = { product_id, category_id: Number(category_id), title, content, params, video_url, specs_book_url, product_imgs };
      const updateData = {};
      for (const key in bodyParams) {
        if (bodyParams[key] !== record[key]) {
          updateData[key] = bodyParams[key];
        }
      }
      const result = await Product_sql.update(updateData, { where: { product_id: product_id } });
      if (!result) {
        return res.json({ code: -1, message: "修改产品失败" });
      }
      res.json({ code: 1, message: "修改产品成功" });
    } catch (error) {
      console.error(error);
      res.json({ code: 500, message: "服务器错误" });
    }
  },
  // 删除产品
  async product_delete(req, res) {
    const { product_id } = req.query;
    try {
      const result = await Product_sql.destroy({ where: { product_id: product_id } });
      if (!result) {
        return res.json({ code: -1, message: "删除产品失败" });
      }
      res.json({ code: 1, message: "删除产品成功" });
    } catch (error) {
      console.error(error);
      res.json({ code: 500, message: "服务器错误" });
    }
  },
};

module.exports = product;
